package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.mapper.UserTagsMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.apache.dubbo.config.spring.util.DubboBeanUtils.log;

@Repository
@Slf4j
public class UserGroupRepositoryImpl implements UserGroupRepository {

    @Autowired
    private UserGroupMapper userGroupMapper;
    @Autowired
    private UserTagsMapper userTagsMapper;
    // Redis key 前缀
    private static final String REDIS_KEY_PREFIX = "user_group:";
    // 缓存过期时间：30分钟
    private static final long REDIS_EXPIRE_TIME = 30;
    private static final TimeUnit REDIS_EXPIRE_UNIT = TimeUnit.MINUTES;

    @Override
    public Integer saveUserGroup(UserGroupPO userGroupPo) {
        return userGroupMapper.insert(userGroupPo);
    }
    @Override
    public PageResult<UserGroupVO> selectPageList(Integer pageNum, Integer pageSize) {
        log.info("Coup数据层:查询优惠券分页列表:{},{}",pageNum,pageSize);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        // 创建分页对象
        Page<UserGroupPO> page = new Page<>(pageNum,pageSize);
        // 执行分页查询
        IPage<UserGroupPO> result = userGroupMapper.selectPage(page, null);
        log.info("Coupon数据层:查询优惠券分页列表:{}",result);
        // 转换为业务层需要的格式
        PageResult<UserGroupVO> pageResult = new PageResult<>(pageNum, pageSize);
        pageResult.setTotal(result.getTotal());
        List<UserGroupVO> voList = result.getRecords().stream()
                .map(po -> {
                    UserGroupVO vo = new UserGroupVO();
                    BeanUtils.copyProperties(po, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        pageResult.setObjects(voList);
        return pageResult;
    }

    @Override
    public UserGroupVO selectUserGroupById(Integer userGroupId) {
        UserGroupPO userGroupPO = userGroupMapper.selectById(userGroupId);
        //select id,tag_type,tag_name,tag_parent_id,coding,status,usage_count,tag_desc,create_at,update_at
        // from lbs_tag_library
        // join lbs_user_group on lbs_tag_library.id=lbs_user_group.tag_ids
        // where tag_ids=#{id}
        if (userGroupPO == null) {
            return null;
        }

        UserGroupVO userGroupVO = new UserGroupVO();
        BeanUtils.copyProperties(userGroupPO, userGroupVO);
        return userGroupVO;
    }


}